<template>
    <el-dialog class="content" :title="title" :modal-append-to-body="false" :visible.sync="isShow" width="30%"
        :lock-scroll="true" :close-on-click-modal="false">
        <template>
            <download-excel class="export-excel-wrapper " :data="data1" :fields="json_fields" :header="header"
                :name="`${fileName}_${title}.xls`">
                <el-button type="primary" @click="excelExport">导出excel</el-button>
            </download-excel>
        </template>
        <el-button type="primary" @click="csvExport" class="item">导出csv</el-button>
    </el-dialog>
</template>

<script>

export default {
    data() {
        return {
            type_id: null,
            title: '',
            isShow: false,
            data1: null,
            header: '',
            json_fields: {
                "特征值": "特征值",
                "nickname": "nickname",
                "值": 'value',
                "时间": 'time'
            }
        }
    },
    props: ['sensorInfo'],
    computed: {
        fileName() {
            return this.sensorInfo.sensor_name + this.sensorInfo.direction
        }
    },
    methods: {
        downloadData(a, data) {
            this.type_id = a
            if (a == 1) {
                //特征值类型的数据
                this.title = '特征值数据'
                this.header = this.fileName
                let selected = data.tagsSelected
                let list = data.list
                let arr = []
                selected.forEach(e => {
                    let nickname = e.nickname
                    list[nickname].forEach(a => {
                        a.nickname = nickname
                        a['eigenvalue'] = e.value
                        arr.push(a)
                    })
                });
                this.json_fields = {
                    "特征值": "eigenvalue",
                    "feature_key": "nickname",
                    "值": 'value',
                    "时间": 'time'
                }
                this.data1 = arr
            }
            else {
                let time = data.timeSelected
                this.header = time

                // 波形或者频谱类数据
                let nickname = data.tagsSelected.nickname
                this.title = data.tagsSelected.value
                let arr = []
                if (this.title.includes("波形")) {
                    let fs = Number(this.sensorInfo.fs)
                    for (var i = 0; i < data.list.maxlen; i++) {
                        arr.push({
                            data: data.list[nickname].data[i],
                            time: ((1 / fs) * 1000 * i).toFixed(3)
                        })
                    }
                    this.json_fields = {
                        "时间/ms": 'time',
                        '值': 'data'
                    }
                } else {
                    data.list[nickname + 'Data'].data.forEach((e1, i1) => {
                        arr.push({
                            'fre': e1,//频率，峰值
                            'data': data.list[nickname + 'Fre'].data[i1]
                        })
                    })
                    this.json_fields = {
                        "频率": 'data',
                        '峰值': 'fre',
                    }
                }
                this.data1 = arr
            }
            this.isShow = true
        },
        excelExport() {
            this.isShow = false
        },
        csvExport() {
            //处理数据
            let data = this.data1
            var dataKey = Object.values(this.json_fields)
            var title = Object.keys(this.json_fields)
            let str = []
            str.push(this.header + '\t\r\n')
            str.push(title.join(',') + '\r\n')
            for (let i = 0; i < data.length; i++) {
                let temp = []
                for (let j = 0; j < dataKey.length; j++) {
                    let a = data[i][dataKey[j]]
                    if (dataKey[j] == 'time')
                        a = a + '\t'
                    temp.push(a)
                }
                str.push(temp.join(',') + '\r\n')
            }
            const blob = new Blob(['\uFEFF' + str.join('')], {
                type: 'test/csv;charset=utf-8',
            })
            //导出
            const url = window.URL.createObjectURL(blob)
            const downloadLink = document.createElement('a')
            downloadLink.href = url
            //导出文件的文件名
            downloadLink.download = `${this.fileName}_${this.title}.csv`
            downloadLink.click()
            window.URL.revokeObjectURL(url)
            this.isShow = false
        },

    },

}
</script>

<style lang="less" scoped>
.content {
    z-index: 9999;

    .item {
        margin-top: 14px;
    }
}
</style>
